//
//  XMSearchHistoryView.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/17.
//

#import "XMSearchHistoryView.h"

@interface XMSearchHistoryView ()

@property (nonatomic, strong) UIView *historyBgView;

@property (nonatomic, copy) NSString *historyKey;

@end

@implementation XMSearchHistoryView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xffffff);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(26.0), self.width - KScaleWidth(16.0), KScaleWidth(26.0)) font:KFontSize(18.0) textColor:kColorHEX(0x333333)];
    titleLabel.text = @"历史搜索";
    [self addSubview:titleLabel];
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.width - KScaleWidth(50.0), titleLabel.y, KScaleWidth(50.0), titleLabel.height) image:@"dele" selectImg:@"dele"];
    [clearBtn addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:clearBtn];
    
    [self addSubview:self.historyBgView];
}

- (void)setContentWithHistoryKey:(NSString *)historyKey {
    _historyKey = historyKey;
    NSString *searchHistoryStr = [[NSUserDefaults standardUserDefaults] objectForKey:historyKey];
    NSArray *historyArr = [searchHistoryStr componentsSeparatedByString:@","];
    CGFloat frameX = 0;
    CGFloat frameY = 0;
    for (int i = 0; i < historyArr.count; i ++) {
        NSString *searchKey = historyArr[i];
        CGFloat width = [searchKey getWidthWithFont:KScaleWidth(15.0)] + KScaleWidth(20.0);
        if (frameX + width > self.width) {
            frameX = 0;
            frameY += KScaleWidth(34.0);
        }
        UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(frameX, frameY, width, KScaleWidth(34.0)) title:searchKey titleColor:kColorHEX(0x666666)];
        [itemBtn addTarget:self action:@selector(itemSearchAction:) forControlEvents:UIControlEventTouchUpInside];
        [_historyBgView addSubview:itemBtn];
        frameX += itemBtn.width;
    }
}
 //保存搜索历史
- (void)saveSearchHistoryWithSearchKey:(NSString *)searchKey {
    NSString *historySearchKey = [[NSUserDefaults standardUserDefaults] objectForKey:_historyKey];
    NSArray *searchKeyArr = [historySearchKey componentsSeparatedByString:@","];
    if (![searchKeyArr containsObject:searchKey]) {
        if (searchKeyArr.count > 0) {
            historySearchKey = [NSString stringWithFormat:@"%@,%@", searchKey, historySearchKey];
        }
        else {
            historySearchKey = searchKey;
        }
    }
    [[NSUserDefaults standardUserDefaults] setObject:historySearchKey forKey:_historyKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//历史搜索点击
- (void)itemSearchAction:(UIButton *)button {
    [self endEditing:YES];
    self.historyItemSelectBlock ? self.historyItemSelectBlock(button.titleLabel.text) : nil;
}

//清空
- (void)clearAction:(UIButton *)button {
    for (UIView *subView in self.historyBgView.subviews) {
        [subView removeFromSuperview];
    }
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:_historyKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (UIView *)historyBgView {
    if (!_historyBgView) {
        _historyBgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(26.0)*2 + KScaleWidth(16.0), self.width, KScaleWidth(70.0))];
    }
    return _historyBgView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
